import streamlit as st
import pandas as pd
import json

st.title("Swe-bench 评估结果可视化")

uploaded = st.file_uploader("上传评估结果 JSON 文件", type=["json"])
if uploaded:
    data = json.load(uploaded)

    # 1. 如果是“汇总型”顶层 dict，直接展示关键指标
    if "total_instances" in data:
        st.subheader("📊 汇总指标")
        cols = st.columns(4)
        cols[0].metric("总实例", data.get("total_instances"))
        cols[1].metric("已提交", data.get("submitted_instances"))
        cols[2].metric("已完成", data.get("completed_instances"))
        cols[3].metric("已解决", data.get("resolved_instances"))

        st.subheader("✅ 已完成 ID 列表")
        st.dataframe(pd.Series(data["completed_ids"], name="task_id"))

        st.subheader("❌ 未完成 ID 列表")
        st.dataframe(pd.Series(data["incomplete_ids"], name="task_id"))

        st.subheader("👍 已解决 ID 列表")
        st.dataframe(pd.Series(data["resolved_ids"], name="task_id"))

    # 2. 如果是“单条详情”数组/列表，再按原逻辑表格化
    elif isinstance(data, list):
        df = pd.DataFrame(data)
        st.subheader("通过率概览")
        if "repo" in df.columns and "passed" in df.columns:
            st.dataframe(df.groupby("repo")["passed"].mean().reset_index())
        if "error_type" in df.columns:
            st.subheader("错误分布")
            st.bar_chart(df["error_type"].value_counts())
    else:
        st.warning("未知 JSON 结构，无法自动可视化")


# 运行命令：streamlit run D:\JunTuan\project\py_test\swebench使用\swe-bench-verified\评估结果可视化.py
